package poem_info;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Task {
    public static void main(String[] args) throws Exception{
        //Jsoup解析含有古诗的页面的所有url
        Document doc = Jsoup.parse(new URL("https://so.gushiwen.cn/shiwens/"), 10000);
        Elements elemenets = doc.select("#right1 > div.cont > a");
        for (Element elemenet : elemenets) {
            //获取每个类别诗歌的url页面，里面包含了每首诗歌的详情页url
            String href = "https://so.gushiwen.cn/" + elemenet.attr("href");
            String type = elemenet.text();
            System.out.println(type + "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
            //解析详情页url
            Document document = Jsoup.parse(new URL(href), 10000);
            //typecont > span[href]
            Elements ele1 = document.select("div.typecont > span");
            //小学古诗\初中古诗\高中古诗\小学文言\初中文言\高中文言 中古诗详情页url是完整
            List<String> types = new ArrayList<String>();types.add("小学古诗");types.add("初中古诗");types.add("高中古诗");types.add("小学文言");types.add("初中文言");types.add("高中文言");
            List<Poem> poemList = new ArrayList<Poem>();
            //创建ParseDetailedUrl对象
            ParseDetailedUrl pdUrl = new ParseDetailedUrl();
            for (Element element1 : ele1) {
                String href1 = null;
                href1 = element1.select("a").attr("href");
                if(!types.contains(type)) {
                    href1 = "https://so.gushiwen.cn" + href1;
                }
                pdUrl.parseUrl(href1, poemList);
            }
            System.out.println(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
        }
    }
}
